<link href="/res/third/select2/css/select2.min.css" rel="stylesheet">
<style>
.layui-layer-content{
	height: auto;
}
 .ui-dialog-close{width: 25px;}
</style>
<div class="caiWuKuang">
	<div>
		<form id="bankInfoForm" class="form-horizontal">
			<input type="hidden" name="companyId" id="bankcompId" /> <input
				type="hidden" name="openingBank" id="openingBank" /> <input
				type="hidden" name="bankName" id="bankName" /> <input type="hidden"
				name="cityName" id="cityName" /> [#if bank??] <input name="id"
				type="hidden" id="companyBankId" value="${bank.id!}" /> <input
				name="ifDefault" type="hidden" id="ifDefault"
				value="${bank.ifDefault!}" /> [/#if]
			<div class="lableIterm">
							<label><span>*</span>账户名称：</label>
							<input type="text" placeholder="请输入账户名称" name="creditName" required
					value="[#if bank??]${bank.creditName!}[/#if]"/>
						</div>
						<div class="lableIterm">
							<label><span>*</span>账号：</label>
							<input type="text" placeholder="请输入账号" name="bankCardNumber" required contentType="int"
								   value="[#if bank??]${bank.bankCardNumber!}[/#if]" />
						</div>
						<div class="lableIterm">
							 <label><span>*</span>账户类型：</label>
							 <input type="radio" name="bankType" id="bankType1" value="2" [#if bank?? && bank.bankType?? ][#if bank.bankType==2]checked[/#if][#else]checked[/#if]><label style="width: 45px;" for="bankType1">基本户</label> 
							 <input style="margin-left: 10px;" type="radio" name="bankType" id="bankType2" value="1" [#if bank?? && bank.bankType?? ][#if	bank.bankType==1]checked[/#if][/#if]><label style="width: 45px;" for="bankType2">结算户</label> 
						</div>
						<div class="lableIterm">
							 <label><span>*</span>开户行所在地：</label>
							 <div class="ppSelect">
			                    <select name="province" required
										id="finance_province"
										value="[#if bank??]${bank.province!}[/#if]">
			                    </select>
			                </div>
			                 <div class="ppSelect">
			                    <select name="city" required
										id="finance_city" value="[#if bank??]${bank.city!}[/#if]">
			                    </select>
			                </div> 
						</div>
						<div class="lableIterm">
							 <label><span>*</span>银行名称：</label>
							 <div class="ppSelect" style="width: 420px;">
			                    <select name="bankCode" required id="bankCode" >
			                         <option value="">请选择银行名称</option>
									 [#if banks??]
										 [#list banks as b]
											<option value="${b.code!}" [#if bank?? && ((bank.bankCode?? && bank.bankCode==b.code) || (bank.bankName == b.name))]selected[/#if] >${b.name!}</option>
										 [/#list]
									 [/#if]
			                    </select>
			                </div> 
						</div>
						<div class="lableIterm">
							<label><span>*</span>开户行：</label>
							<div class="ppSelect" style="width: 420px;border: none;">
								<select  name="openingBankCode" required id="openingBankCode">
									<option value="">请选择开户行</option>
									[#if bankCodes??]
									[#list bankCodes as bc]
										<option value="${bc.code!}" [#if bank?? && ((bank.openingBankCode?? && bank.openingBankCode==bc.code) || (bank.openingBank?? && bc.name?contains(bank.openingBank))) ] selected [/#if]>${bc.name!}</option>
									[/#list] [/#if]
								</select>
							</div>
						</div>
						<div class="lableIterm">
							<label></label>
							<button class="saveCaiWu" type="button" onclick="saveBank();">保存</button>
							<button class="cancleCaiWu" type="button" onclick="layer.closeAll();return false;">取消</button>
						</div>
		</form>
	</div>
</div>
<script src="/res/third/select2/js/select2.full.min.js" type="text/javascript"></script>
<script src="/res/third/select2/js/i18n/zh-CN.js" type="text/javascript"></script>
<script type="text/javascript">
	$(function(){
		new newAddress('finance' , 'select' , 1 ,  1 , 2  [#if bank??],1[/#if]);
		$("#bankCode , select[id^='finance']").on('change' , function(){
			setTimeout(function () {
				var provinceCode = $("#finance_province").val();
				var areaCode = $("#finance_city").val();
				var bankCode = $("#bankCode").val();
				$("#openingBankCode").children().remove();
//				$("#openingBankCode").append("<option value=''>请选择开户行</option>");
				if(areaCode!= '' && bankCode!='' && provinceCode != ''){
					$.ajax({
						url : '/queryOpenBank' ,
						type : 'post' ,
						dataTyep : 'json' ,
						data : {areaCode : areaCode , bankCode : bankCode} ,
						success : function(data){
							if(data){
								var openBanks = $.parseJSON(data);
								var options = [];
								$.each(openBanks ,function(n , ob){
//									$("#openingBankCode").append("<option value='"+ob.code+"'>"+ob.name+"</option> ");
									options.push({id : ob.code, text : ob.name});
								});
//
								$("#openingBankCode").select2({
									data: options,
									placeholder:'请选择开户行',
									allowClear:true
								});

							}
						}
					});
				}
			} ,500);
		});

        $("#openingBankCode").select2({});
	});
	
	
	/**保存财务信息功能*/
	function saveBank(){
		var validate = true;
		
		/*js验证*/
		$("#bankInfoForm input ,#bankInfoForm select ,#bankInfoForm textarea ").each(function(){
			if($(this).attr("required") !== undefined && $(this).val().trim() == ""){
				console.log(this);
				layer.tips('不能为空，请填写！', $(this) , {tips: [2 ,'#fb4047']});
				$(this).focus();
				validate = false;
				return false;
			}
			if(($(this).attr("contentType") !== undefined)){
//				if($(this).attr("contentType") == 'numeric'){
//					if(!(numReg).test($(this).val())){
//						layer.tips('请填写正确的金额！', $(this) , {tips: [2 ,'#fb4047']});
////						gotoposition($(this));
//						validate = false;
//						return false;
//					}
//				}
				if($(this).attr("contentType") == 'int'){
					if(!(/^\d*$/).test($(this).val())){
						layer.tips('请填写正确的账号！', $(this) , {tips: [2 ,'#fb4047']});
//						gotoposition($(this));
						validate = false;
						return false;
					}
				}
			}
		});

		
		/*验证结束*/
		
		if(validate){
			var lding = layer.load(2);
			$("#openingBank").val($("#openingBankCode option:selected").text());
			$('#bankName').val($("#bankCode option:selected").text());
			$('#cityName').val($("#finance_city option:selected").text());
			$('#bankcompId').val($("#company_id").val());
			$.ajax({
				url :  '/company/saveBank' , 
				type : 'post' , 
				dataType : 'json' , 
				data : $("#bankInfoForm").serialize(),
				success : function(data){
					layer.close(lding);
					if(data.success){
                        layer.closeAll();
						layer.confirm('财务信息保存成功！小通将在第一时间为您审核，请耐心等待！', {icon: 1, title:'提示'}, function(index){
							location.reload();
						} , function(index){
							location.reload();
						});
					}else{
						layer.msg("财务信息保存失败："+data.msg , {time:2000} );
					}
					$(".layui-layer-setwin").hide();
					$(".layui-layer-btn1").hide();
				}
			});
		}
		
				
	}
</script>